// JavaScript Document
$(document).ready(function() {    
    //ajaxform
    var options = { 
        beforeSubmit:showRequest,//表单验证方式
        success:showResponse  // post-submit callback 
    }; 

    $('.ajax').ajaxForm(options); 

    function showResponse(responseText, statusText)  {
        $("#loading").hide();
        if (statusText == 'success') {
            var responseText=eval('('+responseText+')');
            if(responseText.state != 'SUCCESS'){
                responseText.message = responseText.state;
            }
            _alert(responseText.message,responseText.url);                     
        }else{
            _alert('服务器错误');
        }
    } 

    function showRequest(){
        FormStatus = $("#doc-vld-msg").validator('isFormValid');
        if (FormStatus){
            $("#loading").show();
        }
        return FormStatus;
    }
});

// JavaScript Document
(function($) {
  if ($.AMUI && $.AMUI.validator) {
    // 增加单个正则
    $.AMUI.validator.patterns.mobile = /^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|189[0-9]{8}$/;
  }
})(window.jQuery);

$(function() {
    $('#doc-vld-msg').validator({
        //通过时回调
        onValid: function(validity) {
            $(validity.field).closest('.am-form-group').find('.am-alert').hide();
        },

        //异常时回调
        onInValid: function(validity) {
          var $field = $(validity.field);
          var $group = $field.closest('.am-form-group');
          var $alert = $group.find('.am-alert');
          // 使用自定义的提示信息 或 插件内置的提示信息
          var msg = $field.data('validationMessage') || this.getValidationMessage(validity);
          if (!$alert.length) {
            $alert = $('<div class="am-alert am-alert-danger"></div>').hide().appendTo($group);
          }
          $alert.html(msg).show();
        },
  });
});


//重写alert弹窗
function _alert(msg,url){
	var title=title?title:"消息提示";
	var html='<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">\
				  <div class="am-modal-dialog">\
					<div class="am-modal-hd">消息提示</div>\
					<div class="am-modal-bd">'+msg+'</div>\
					<div class="am-modal-footer">\
					  <span class="am-modal-btn">确定</span>\
					</div>\
				  </div>\
				</div>';
	$(document.body).append(html);
	var $modal=$("#my-alert");
	$modal.modal('open').on("closed.modal.amui",function(){
		$modal.remove();
		if(url!='' && url!=undefined && url!="undefined"){
			window.location.href = url;		
		}
	});	
}


function checkMobile(v){
    if(v==""){
        return false;
    }else{
        var pattern = /^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|189[0-9]{8}$/;
        flag = pattern.test(v);
        if(!flag){
            return false;
        }else{
            return true;
            }
    }
}

/*提示登录*/
function login(){
    var _html = '<a href="index.php?m=Register&a=login"><h3>请先登录</h3></a>';
    _alert(_html,'index.php?m=Register&a=login'); 
}

/*加入购物车*/
function addCart(goodsID){
    $("#loading").show();
    $.get("index.php?m=Store&a=addcart&goodsID="+goodsID+"&temp="+new Date().getTime(),function(response){
        $("#loading").hide();
        _alert(response);
        v = parseInt($('#cartNum').html())+1;
        $('#cartNum').html(v);
    }); 
}

/* 购物车数量 */
function changeNum(type,cartID){
    danjia = parseFloat($("#goodsPrice"+cartID).val());
    total = parseFloat($("#totalMoney").html());
    oldNum = parseInt($("#buyNum"+cartID).html());

    if (type=='add') {
        v = oldNum+1;
    }else if(type="del"){
        v = oldNum-1;
        if (v<1) {
          v=1;
        }    
    }
    
    if (v != oldNum){
        $.get("index.php?m=Store&a=setCartNum&number="+v+"&cartID="+cartID+"&temp="+new Date().getTime(),function(response){}); 
    }   

    $("#buyNum"+cartID).html(v);    
    total = (total-danjia*oldNum)+v*danjia;
    
    $("#totalMoney").html(total.toFixed(2));
}

/* 加入收藏 */
function addFav(goodsID,goodsName,goodsPrice,goodsPic){
    $("#loading").show();
    $.get("index.php?m=Store&a=addfav&goodsID="+goodsID+"&name="+goodsName+"&price="+goodsPrice+"&picname="+goodsPic+"&temp="+new Date().getTime(),function(response){
        $("#loading").hide();
        _alert(response);
    });
}

//弹出规格选择窗
function buyBtnEvent(evt){
    if (evt=="add"){
        $("#modal-btn").val("加入购物车");
        $("#action").val("add");
    }else if(evt=="buy"){       
        $("#modal-btn").val("下一步");
        $("#action").val("buy");
    }

    $('#cartModel').modal();

    $('#cartModel').on('closed.modal.amui', function() {
        //$(this).removeData("amui.modal");

        //移除样式
        $(".spec_items a").each(function(){
            $(this).removeClass("current");
        })

        //选择参数清空
        $("#optionid").val("");
    }); 
}